//
//  SMyuController.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/3/7.
//  Copyright © 2016年 game. All rights reserved.
//

#import "SMyuController.h"
#import "SelectNavView.h"
#import "SFLeixingCell.h"

#import "SMyuTabHead.h"

#import "SFTabHeadView.h"
#import "CGFdbmBase.h"
#import "PageModels.h"
#import "FishModel.h"

@interface SMyuController ()<UITableViewDataSource,UITableViewDelegate,SFLeixingCellClick>

@property (nonatomic,strong) SelectNavView *naV;

@property (nonatomic,strong) UITableView *tableview;

@property (nonatomic,assign) CGFloat cellHight;

@property (nonatomic,strong) SMyuTabHead *Tabheard;

@property (nonatomic,strong) PageModels *pageModel;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NSString *idString;

@end

@implementation SMyuController

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray= [NSMutableArray array];
    }
    return _dataArray;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.app.myTabBar.tabBar.hidden = YES;
    [self CreatNavView];
}
-(void)CreatNavView{
    self.naV = [[SelectNavView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    self.naV.titile.text = @"选择鱼类";
    self.naV.share.titleLabel.font = font(SCREEN_WIDTH*0.05);
    [self.naV.share setTitle:@"确定" forState:UIControlStateNormal];
    self.naV.share.y = 21;
    self.naV.share.x = SCREEN_WIDTH - 60;
    [self.view addSubview:self.naV];
    [self.naV.bacBtn addTarget:self action:@selector(Back) forControlEvents:UIControlEventTouchUpInside];
    [self.naV.share addTarget:self action:@selector(Tijiao) forControlEvents:UIControlEventTouchUpInside];
}
-(void)Back{
    NSArray *viewcontrollers=self.navigationController.viewControllers;
    if (viewcontrollers.count>1) {
        if ([viewcontrollers objectAtIndex:viewcontrollers.count-1]==self) {//push方式
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else{//present方式
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
-(void)Tijiao{
    
    if (_SMyuBlock) {
        _SMyuBlock(self.rerult,self.idString);
    }
    
    NSArray *viewcontrollers=self.navigationController.viewControllers;
    if (viewcontrollers.count>1) {
        if ([viewcontrollers objectAtIndex:viewcontrollers.count-1]==self) {//push方式
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else{//present方式
        [self dismissViewControllerAnimated:YES completion:nil];
    }

    
}

-(void)DataFromSQL{
    [CGFdbmBase OpenSQlWhiPathName:@"FirstPage.db"];
    NSInteger TableCount = [CGFdbmBase getTableItemCount:@"FirstPage"];
    
    if (TableCount!=0) {
        [self.dataArray addObjectsFromArray:[CGFdbmBase ListWhith:@"FirstPage" dicName:@"Dic"]];
        self.pageModel = [[PageModels alloc]initWithDictionary:_dataArray.firstObject error:nil];
    }
    [_tableview reloadData];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self DataFromSQL];
    
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    
    [_tableview registerNib:[UINib nibWithNibName:@"SFLeixingCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:_tableview];
    
    _tableview.tableFooterView = [UIView new];
    
    self.Tabheard = [[NSBundle mainBundle]loadNibNamed:@"SMyuTabHead" owner:nil options:nil].lastObject;
    self.Tabheard.frame = CGRectMake(0, 0, SCREEN_WIDTH, 55);
    self.Tabheard.text.text = self.rerult;
    _tableview.tableHeaderView = self.Tabheard;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 45;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    SFTabHeadView *head  = [[NSBundle mainBundle]loadNibNamed:@"SFTabHeadView" owner:nil options:nil].lastObject;
    head.frame = CGRectMake(0, 0, SCREEN_WIDTH, 35);
    
    FishModel *model = self.pageModel.fish[section];
    head.title.text = model.name;
    head.Shoufei.alpha = 0;
    
    return head;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    tableView.separatorStyle = 0;
    
    SFLeixingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
     cell.isYulei = YES;
    FishModel *model = self.pageModel.fish[indexPath.section];
    
    cell.resultString = self.rerult;
    cell.charactAry = model.data;
    self.cellHight = cell.cellhight;
    cell.deleget = self;
   
    
    return cell;
}

/**
 * 点击cell上的btn所调代理方法
 *
 *  @param result 鱼的品种选择结果
 */
-(void)IsYuLeiClick:(NSString *)result AndIdString:(NSString *)idString{
    self.Tabheard.text.text = result;
    self.rerult = result;
    self.idString = idString;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.cellHight + 0.1;
}

@end
